<script setup >
import { ref, reactive } from "vue";
import { NButton } from "naive-ui";
import { orderStatus } from "@/api/constants";
import { usePayTime } from "@/utils";

const emit = defineEmits([
  "on-cancel",
  "on-delete",
  "on-confirm",
  "on-logistics",
]);
const props = defineProps({
  order: {
    type: Object,
    default: {},
  },
});

const { start, timeText } = usePayTime();
start(props.order.countdown);
</script>

<template>
  <div class="order-list">
    <div class="order-item">
      <div class="head">
        <span>下单时间：{{ order.createTime }}</span>
        <span>订单编号：{{ order.id }}</span>
        <span class="down-time" v-if="order.orderState === 1">
          <i class="iconfont icon-down-time"></i>
          <b v-if="order.countdown > -1">付款截止：{{ timeText }}</b>
        </span>
        <a
          href="javascript:;"
          class="del"
          v-if="[5, 6].includes(order.orderState)"
          @click="$emit('on-delete', order)"
          >删除</a
        >
      </div>
      <div class="body">
        <div class="column goods">
          <ul>
            <li v-for="goods in order.skus" :key="goods.id">
              <router-link class="image" :to="`/product/${goods.spuId}`">
                <img v-lazy="goods.image" alt="" />
              </router-link>
              <div class="info">
                <p class="name ellipse2">
                  {{ goods.name }}
                </p>
                <p class="attr ellipse">
                  {{ goods.attrsText }}
                </p>
              </div>
              <div class="price">¥{{ goods.realPay }}</div>
              <div class="count">x{{ goods.quantity }}</div>
            </li>
          </ul>
        </div>
        <div class="column state">
          <p>{{ orderStatus[order.orderState].label }}</p>
          <p v-if="order.orderState === 3">
            <a
              @click="$emit('on-logistics', order)"
              href="javascript:;"
              class="green"
              >查看物流</a
            >
          </p>
          <p v-if="order.orderState === 4">
            <a href="javascript:;" class="green">评价商品</a>
          </p>
          <p v-if="order.orderState === 5">
            <a href="javascript:;" class="green">查看评价</a>
          </p>
        </div>
        <div class="column amount">
          <p class="red">¥ {{ order.payMoney.toFixed(2) }}</p>
          <p>（含运费：¥ {{ order.postFee.toFixed(2) }}）</p>
          <p>在线支付</p>
        </div>
        <div class="column action">
          <n-button
            @click="$router.push(`/member/pay?orderId=${order.id}`)"
            v-if="order.orderState === 1"
            type="primary"
            size="small"
            >立即付款</n-button
          >
          <n-button
            @click="$emit('on-confirm', order)"
            v-if="order.orderState === 3"
            type="primary"
            size="small"
            >确认收货</n-button
          >
          <p>
            <a @click="$router.push(`/member/order/${order.id}`)">查看详情</a>
          </p>
          <p v-if="order.orderState === 1">
            <a @click="$emit('on-cancel', order)" href="javascript:;"
              >取消订单</a
            >
          </p>
          <p v-if="[2, 3, 4, 5].includes(order.orderState)">
            <a
              @click="$router.push(`/member/checkout?orderId=${order.id}`)"
              href="javascript:;"
              >再次购买</a
            >
          </p>
          <p v-if="[4, 5].includes(order.orderState)">
            <a href="javascript:;">申请售后</a>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang='less'>
.order-list {
  background: #fff;
  margin: 20px;
}
.order-item {
  margin-bottom: 20px;
  border: 1px solid #f5f5f5;
  .head {
    height: 50px;
    line-height: 50px;
    background: #f5f5f5;
    padding: 0 20px;
    overflow: hidden;
    span {
      margin-right: 20px;
      &.down-time {
        margin-right: 0;
        float: right;
        i {
          vertical-align: middle;
          margin-right: 3px;
        }
        b {
          vertical-align: middle;
          font-weight: normal;
        }
      }
    }
    .del {
      margin-right: 0;
      float: right;
      color: #999;
    }
  }
  .body {
    display: flex;
    align-items: stretch;
    .column {
      border-left: 1px solid #f5f5f5;
      text-align: center;
      padding: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      > p {
      }
      &:first-child {
        border-left: none;
      }
      &.goods {
        flex: 1;
        padding: 0;
        align-items: center;
        ul {
          li {
            border-bottom: 1px solid #f5f5f5;
            padding: 10px;
            display: flex;
            &:last-child {
              border-bottom: none;
            }
            img {
              width: 70px;
              height: 70px;
              border: 1px solid #f5f5f5;
            }
            .info {
              width: 220px;
              text-align: left;
              padding: 0 10px;
              p {
                margin-bottom: 5px;
                &.name {
                  height: 45px;
                }
                &.attr {
                  color: #999;
                  font-size: 12px;
                  span {
                    margin-right: 5px;
                  }
                }
              }
            }
            .price {
              width: 100px;
              display: flex;
              align-items: center;
              justify-content: center;
            }
            .count {
              width: 80px;
              display: flex;
              align-items: center;
              justify-content: center;
            }
          }
        }
      }
      &.state {
        display: flex;
        flex-flow: column;
        align-items: center;
        justify-content: center;
        width: 120px;
        .green {
          color: @xtxColor;
        }
      }
      &.amount {
        display: flex;
        flex-flow: column;
        align-items: center;
        justify-content: center;
        width: 200px;
        p {
          margin-top: 5px;
        }
        .red {
          color: @priceColor;
        }
      }
      &.action {
        width: 140px;
        display: flex;
        flex-flow: column;
        align-items: center;
        justify-content: center;

        a {
          display: block;
          cursor: pointer;
          margin-top: 10px;
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
</style>